package engine.graphics.popup;

import util.List;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import engine.data.Building;
import engine.data.City;
import engine.data.Unit;
import engine.data.UnitType;
import engine.graphics.MapCanvasEngine;
import engine.graphics.misc.GraphicsCustomFont;

public final class PopupProduction extends TabPopup {

	private static final byte BUILDINGS_PANEL = 1;

	private static final byte UNITS_PANEL = 2;

	private static int[][][] buildingsCursor;

	private static int[][][] unitsCursor;

	private List buildings;

	private List units;

	public City city;

	private byte vBuildingsIndex = 0;

	private byte vBuildingsSubIndex = 0;

	private byte vUnitsIndex = 0;

	private byte vUnitsSubIndex = 0;

	public Class selectedType = null;

	public byte selectedId = -1;

	public PopupProduction(final City selectedCity, final List selectedBuildings, final List selectedUnits) {
		tabCursor = new int[][][] { { { 1, 1 }, { 17, 11 } }, { { 20, 1 }, { 48, 11 } }, { { 70, 1 }, { 28, 11 } } };
		this.city = selectedCity;
		this.buildings = selectedBuildings;
		this.units = selectedUnits;
		buildingsCursor = initCursor((byte) this.buildings.size(), (byte) 10);
		unitsCursor = initCursor((byte) this.units.size(), (byte) 10);
	}

	public void show(final int screenX, final int screenY) {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		super.show(screenX, screenY, graphics);
		GraphicsCustomFont.drawString(graphics, "[X] Buildings Units", this.x + 2, this.y + 3);
		this.hIndex = BUILDINGS_PANEL;
		showHCursor();
		showPanel();
	}

	public void showBuildingsPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		byte max = (byte) Math.min(this.buildings.size(), ((vBuildingsSubIndex + 1) * 11));
		short pp;
		short turns;
		short bp = this.city.getBonusProduction();
		for (byte i = (byte) (vBuildingsSubIndex * 11); i < max; i++) {
			GraphicsCustomFont.drawString(graphics, Building.getName(((Byte) this.buildings.get(i)).byteValue()),
					this.x + 5, this.y + j);
			pp = (short) Math.max(0, this.city.getBuildingPP() - this.city.productionBox);
			turns = (short) (pp / bp);
			if ((pp / bp) * bp < pp)
				turns++;
			GraphicsCustomFont.drawString(graphics, "" + turns, this.x + 117, this.y + j);
			j += 10;
		}
		if (this.buildings.size() > 11)
			GraphicsCustomFont.drawString(graphics, "...", this.x + 115, this.y + 125);
		this.panel = BUILDINGS_PANEL;
	}

	public void showUnitsPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		byte max = (byte) Math.min(this.units.size(), ((vUnitsSubIndex + 1) * 11));
		short pp;
		short turns;
		short bp = this.city.getBonusProduction();
		for (byte i = (byte) (vUnitsSubIndex * 11); i < max; i++) {
			GraphicsCustomFont.drawString(graphics, UnitType.getName(((Byte) this.units.get(i)).byteValue()),
					this.x + 5, this.y + j);
			pp = (short) Math.max(0, this.city.getBuildingPP() - this.city.productionBox);
			turns = (short) (pp / bp);
			if ((pp / bp) * bp < pp)
				turns++;
			GraphicsCustomFont.drawString(graphics, "" + turns, this.x + 117, this.y + j);
			j += 10;
		}
		if (this.units.size() > 11)
			GraphicsCustomFont.drawString(graphics, "...", this.x + 115, this.y + 125);
		this.panel = UNITS_PANEL;
	}

	public void keyPressed(final int key) {
		int action = MapCanvasEngine.canvas.getGameAction(key);
		if (action != 0)
			switch (action) {
				case Canvas.DOWN:
					switch (this.panel) {
						case BUILDINGS_PANEL:
							vBuildingsIndex = moveCursor(buildingsCursor, vBuildingsIndex, (byte) 1, (byte) 12);
							break;
						case UNITS_PANEL:
							vBuildingsIndex = moveCursor(unitsCursor, vUnitsIndex, (byte) 1, (byte) 12);
							break;
						default:
							break;
					}
					this.repaint();
					break;
				case Canvas.UP:
					switch (this.panel) {
						case BUILDINGS_PANEL:
							vBuildingsIndex = moveCursor(buildingsCursor, vBuildingsIndex, (byte) -1, (byte) 12);
							break;
						case UNITS_PANEL:
							vBuildingsIndex = moveCursor(unitsCursor, vUnitsIndex, (byte) -1, (byte) 12);
							break;
						default:
							break;
					}
					this.repaint();
					break;
				case Canvas.LEFT:
					this.showNextTab((byte) -1);
					break;
				case Canvas.RIGHT:
					this.showNextTab((byte) 1);
					break;
				case Canvas.FIRE:
					switch (this.panel) {
						case BUILDINGS_PANEL:
							fireBuildingsPanel();
							break;
						case UNITS_PANEL:
							fireUnitsPanel();
							break;
						case CLOSE_PANEL:
							this.hide();
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
	}

	private void fireUnitsPanel() {
		if (vUnitsIndex % 12 == 11 || (vUnitsIndex == unitsCursor.length - 1 && this.units.size() > 11)) {
			vUnitsIndex += 12;
			vUnitsSubIndex++;
			if (vUnitsSubIndex * 11 > this.units.size()) {
				vUnitsSubIndex = 0;
				vUnitsIndex = (byte) (vUnitsIndex % 12);
			}
			showPanel();
			if (vUnitsIndex >= unitsCursor.length)
				vUnitsIndex = (byte) (unitsCursor.length - 1);
			showVCursor(unitsCursor, vUnitsIndex);
		} else {
			if (!hCursorScope) {
				selectedType = Unit.class;
				selectedId = ((Byte) this.units.get((vUnitsSubIndex * 11) + vUnitsIndex % 12)).byteValue();
				this.hide();
			}
		}
	}

	private void fireBuildingsPanel() {
		if (vBuildingsIndex % 12 == 11 || (vBuildingsIndex == buildingsCursor.length - 1 && this.buildings.size() > 11)) {
			vBuildingsIndex += 12;
			vBuildingsSubIndex++;
			if (vBuildingsSubIndex * 11 > this.buildings.size()) {
				vBuildingsSubIndex = 0;
				vBuildingsIndex = 11;
			}
			showPanel();
			if (vBuildingsIndex >= buildingsCursor.length)
				vBuildingsIndex = (byte) (buildingsCursor.length - 1);
			showVCursor(buildingsCursor, vBuildingsIndex);
		} else {
			if (!hCursorScope) {
				selectedType = Building.class;
				selectedId = ((Byte) this.buildings.get((vBuildingsSubIndex * 11) + vBuildingsIndex % 12)).byteValue();
				this.hide();
			}
		}
	}

	protected void showPanel() {
		super.showPanel();
		switch (this.hIndex) {
			case BUILDINGS_PANEL:
				showBuildingsPanel();
				break;
			case UNITS_PANEL:
				showUnitsPanel();
				break;
			default:
				break;
		}
		this.repaint();
	}
}
